package com.yidu.controller;

import com.yidu.entity.VipTb;
import com.yidu.service.VipTbService;
import com.yidu.utils.BaseMessage;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * (VipTb)表控制层
 *
 * @author JiuYue
 * @since 2021-07-29 16:28:37
 */
@RestController
@RequestMapping("vipTb")
public class VipTbController {
    /**
     * 服务对象
     */
    @Resource
    private VipTbService vipTbService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne")
    public VipTb selectOne(Integer id) {
        return this.vipTbService.queryById(id);
    }

    /**
     * 登录的方法
     * @param vipTb
     * @return
     */
    @PostMapping("/findLogin")
    public BaseMessage findLogin(VipTb vipTb){
        VipTb po=vipTbService.findLogin(vipTb);
        if (po==null){
            return BaseMessage.NO("登录失败");
        }else{
            return BaseMessage.YES("登录成功",po);
        }
    }

    /**
     * 注册的方法
     * @param vipTb
     * @return
     */
    @PostMapping("/add")
    public BaseMessage insert(VipTb vipTb){
        VipTb po=vipTbService.insert(vipTb);
        if (po==null){
            return BaseMessage.NO("增加失败");
        }else{
            return BaseMessage.YES("增加成功",po);
        }
    }
    @PostMapping("/checkName")
    public BaseMessage checkName(String vname){
        VipTb po=vipTbService.findByName(vname);
        System.out.println(po);
        if (po==null){
            return BaseMessage.YES("已存在");
        }else {
            return BaseMessage.NO("可注册");
        }
    }
    /**
     * 转换日期的方法
     * @param binder
     */
    @InitBinder
    public void InitBinder(ServletRequestDataBinder binder){
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),
                true));
    }
}
